<template>
	<view class="form card">
		<u--form :model="form" labelPosition="top" labelWidth="150">
			<u-form-item class="formItem" label="" prop="name">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>公司名称</text>
						</view>
						<!-- <u-icon name="arrow-right"></u-icon> -->
					</view>
				</template>
				<u--input v-model="form.name" border="none" placeholder="请填写公司名称"></u--input>
			</u-form-item>
			<u-form-item class="formItem" label="" prop="companyName">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>公司全称</text>
						</view>
						<!-- <u-icon name="arrow-right"></u-icon> -->
					</view>
				</template>
				<u--input v-model="form.companyName" border="none" placeholder="请填写公司名称"></u--input>
			</u-form-item>
			<u-form-item class="formItem" label="公司背景图" prop="title">
				<u-upload :fileList="form.background" @afterRead="afterRead($event,'background')" @delete="deletePic($event,'background')" name="6" multiple
					:maxCount="1" width="100" height="100">
				</u-upload>
			</u-form-item>
			<u-form-item class="formItem" label="公司logo" prop="title">
				<u-upload :fileList="form.logo" @afterRead="afterRead($event,'logo')" @delete="deletePic($event,'logo')" name="6" multiple
					:maxCount="1" width="100" height="100">
				</u-upload>
			</u-form-item>
			<u-form-item class="formItem" label="" prop="address" @click.stop="routerLink('jobAddress2')">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>公司地址</text>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</template>
				<u--input v-model="form.address" border="none" disabled disabledColor="#fff"
					placeholder="请选择公司地址"></u--input>
			</u-form-item>
			<u-form-item class="formItem" label="" prop="compnayHead">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>法定代表人</text>
						</view>
						<!-- <u-icon name="arrow-right"></u-icon> -->
					</view>
				</template>
				<u--input v-model="form.compnayHead" border="none" placeholder="请填写法定代表人"></u--input>
			</u-form-item>
			<u-form-item class="formItem" label="" prop="capital">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>注册资金</text>
						</view>
						<!-- <u-icon name="arrow-right"></u-icon> -->
					</view>
				</template>
				<u--input v-model="form.capital" border="none" placeholder="请填写注册资金"></u--input>
			</u-form-item>
			<u-form-item class="formItem" label="" prop="number">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>信用代码</text>
						</view>
						<!-- <u-icon name="arrow-right"></u-icon> -->
					</view>
				</template>
				<u--input v-model="form.number" border="none" placeholder="请填写信用代码"></u--input>
			</u-form-item>
			<u-form-item class="formItem" label="" prop="setTime">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>成立时间</text>
						</view>
						<!-- <u-icon name="arrow-right"></u-icon> -->
					</view>
				</template>
				<u--input v-model="form.setTime" border="none" placeholder="请选择成立时间"></u--input>
			</u-form-item>
			<u-form-item class="formItem" label="" prop="isOpen">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>经营状态</text>
						</view>
						<!-- <u-icon name="arrow-right"></u-icon> -->
					</view>
				</template>
				<u--input v-model="form.isOpen" border="none" placeholder="请选择经营状态"></u--input>
			</u-form-item>
			<u-form-item class="formItem" label="" prop="brand" @click="showSelectBtn('brand')">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>品牌</text>
						</view>
						<!-- <u-icon name="arrow-right"></u-icon> -->
					</view>
				</template>
				<view class="descInfo" :class="!form.brand?'noData':''">{{getBrand(form.brand)}}</view>
				<!-- <u--input disabled disabledColor="#fff"  border="none" placeholder="请选择品牌">{{getBrand(form.brand)}}</u--input> -->
			</u-form-item>
			<u-form-item class="formItem " label="" prop="type" @click.stop="routerLink('companyDesc')">
				<template #label>
					<view class="r-flex-between-center">
						<view class="">
							<text>*</text>
							<text>公司描述</text>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</template>
				<u-parse :content="form.info" v-if="form.info"></u-parse>
				<u--input v-else border="none" disabled disabledColor="#fff" placeholder="介绍公司" ></u--input>
			</u-form-item>

		</u--form>
		<view class="gysBtn" @click="submitForm">
			保存
		</view>
		<u-action-sheet :show="state.showSelect" :actions="state.actions" title="请选择" description=""
			@close="state.showSelect = false" @select="SelectFun">
		</u-action-sheet>
	</view>
</template>
<script setup lang="ts">
	//发布岗位
	import { onMounted, reactive, ref } from "vue";
	import {
		validatePhoneNumber
	} from '@/utlis/common.js'
	import {
		getCurrentInstance
	} from 'vue'
	import {
		useRouter
	} from 'uni-mini-router'
	const router = useRouter()
	import {
		onShow,
		onHide,
		onLoad
	} from '@dcloudio/uni-app'
	// import form from "../../../uni_modules/uview-plus/libs/config/props/form";
	import {
		userStore
	} from '@/pinia';
	const userStoreInfo = userStore();
	const {
		proxy,
	} = getCurrentInstance()

	let itemId = ref('')
	let marketType = ref([])
	const form = ref({ //表单信息
		name: '',
		companyName: '',
		address: '',
		compnayHead: '',
		capital: '',
		number: '',
		setTime: '',
		isOpen: '',
		info: '',
		logo: [],
		brand:'',
		background:[],
		provinceId:'',
		province:'',
		city:'',
		cityId:'',
		areaName :'',
		areaId:'',
	})
	let state = reactive({
		fileList: [{url:''}],
		type: '',
		showSelect: false,
		actions: [],
		educationBackgroundAction: [
			{ name: '不限', val: '' },
			{ name: '初中及以下', val: 0 },
			{ name: '高中（中专）', val: 1 },
			{ name: '大学（本专科）', val: 2 },
			{ name: '硕士及以上', val: 3 },
			{ name: '高中（中专）', val: 1 },
		],
		workYearAction: [
			{ name: '1年以内', val: 0 },
			{ name: '1-3年', val: 1 },
			{ name: '3-5年', val: 2 },
			{ name: '5-8年', val: 3 },
			{ name: '8年以上', val: 4 },
		],
		brandList:[
			{ name: '个体品牌', val: 1 },
			{ name: '区域品牌', val: 2 },
			{ name: '全国连锁品牌', val: 3 },
			{ name: '国际品牌连锁', val: 4 },
		]
	})

	const showSelectBtn = (type) => {
		state.type = type
		switch (type) {
			case 'brand':
				state.actions = state.brandList
				break;
			case 'educationBackground':
				state.actions = state.educationBackgroundAction
				break;
			case 'workYear':
				state.actions = state.workYearAction
				break;
			default:
				break;
		}

		state.showSelect = true
	}
	const getBrand = (val)=>{
		let idx = state.brandList.findIndex(el=>el.val == val)
		if(idx!=-1){
			return  state.brandList[idx].name
		}else{
			return '请选择'
		}
	}
	const SelectFun = (e) => {
		let type = state.type
		switch(type){
			case 'brand':
				form.value.brand = e.val
				break;
			default:
				break;
		}
	}
	const typeChose = () => {
		let arr = marketType.value.map((item) => {
			return item.name
		})
		// console.log(marketType.value);
		uni.showActionSheet({
			itemList: arr,
			success: function (res) {
				form.value.type = arr[res.tapIndex]
				form.value.typeId = marketType.value[res.tapIndex].id
			},
			fail: function (res) {
				// console.log(res.errMsg);
			}
		});
	}
	const routerLink = (name) => {
		console.log('name='+name)
		if(name == 'companyDesc'){
			uni.$once('companyDescInfo', res => {
				console.log(res,'companyDescInfo');
				form.value.info = res
			})
			router.push({
				name: name,
				params: {
					companyId: companyId.value,
					backUrl: 'companyEdit',
					
				}
			})
		}else{
			uni.$once('companyAddress', res => {
				console.log(res,'companyDescInfo');
				form.value.address = res.address
				form.value.provinceId = res.provinceId
				form.value.province = res.province
				form.value.city = res.city
				form.value.cityId = res.cityId
				form.value.areaName = res.areaName
				form.value.areaId = res.areaId
			})
			router.push({
				name: name,
				params: {
					backUrl: 'companyEdit',
					
				}
			})
		}
		
	}
	const companyId = ref('')
	const pageType = ref('')
	onLoad((options) => {
		companyId.value = options.companyId
		pageType.value = options.type || ''
		getCompanyDetail()
		// getMeiyeRecruitJob2List()
	})

	onMounted(() => {

	})
	// 新增图片
	function afterRead(e,type) {
		// console.log(e.file[0].url);
		uni.uploadFile({
			url: proxy.$uploadUrl + proxy.$api.uploadFile,
			filePath: e.file[0].url,
			name: 'file',
			formData: {
				'file': e.file[0].url,
			},
			success: (res) => {
				console.log(JSON.parse(res.data).result);
				form.value[type] = [{url:JSON.parse(res.data).result}]
			}
		});
		
	}
	// 删除图片
	const deletePic = (event,type) => {
		form.value[type].splice(event.index, 1);
	};
	//获取详情
	const getCompanyDetail = () => {
		proxy.$req('GET', proxy.$api.meiyeCompany2Details, {
			id: companyId.value,
		}).then((res) => {
			if (res.code === 200) {
				res.result.logo = [{ url: res.result.logo }]
				res.result.background = [{ url: res.result.background }]
				form.value = res.result
			}
		})
	}
	// 提交
	const submitForm = () => {
		// console.log(form.value);
		let data = form.value
		data.logo = data.logo[0].url
		data.background = data.background[0].url
		proxy.$req('POST', proxy.$api.meiyeRecruitCompany3Edit, data).then((res) => {
			console.log(res);
			if (res.code === 200) {
				uni.showToast({
					title: '修改成功'
				})
				router.push({
					name: 'companyDetail',
					params: {
						companyId: companyId.value,
						type: pageType.value

					}
				})
			}
		})
	}
</script>
<style lang="scss" scoped>
	page {
		background-color: #F2F2F2;
		padding:20rpx 0;
	}
	.card{
		background:#fff;
		box-sizing: border-box;
		border: 2rpx solid #fff;
		margin: 30rpx auto;
		padding: 24rpx;
		background:#fff;
		border-radius: 24rpx;
		box-shadow: 8rpx 8rpx 32rpx 0rpx rgba(0, 0, 0, 0.04);
	}
	.form {
		width: 686rpx;
		margin: 0 auto;

		.formItem {
			border-bottom: 1rpx solid #e7e7e7;

			.formItemArea {
				width: 100%;
			}
			
			.noData{
				color: rgb(192, 196, 204);
			}
		}
	}

	.gysBtn {
		width: 586rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: #CB645E;
		border-radius: 10rpx;
		color: #fff;
		margin: 50rpx auto;
	}
</style>